#include "user_lib.h"
#include "util/types.h"
#include "util/string.h"

int main(int argc, char *argv[]) {

  int fd0, fd1;

  int MAXBUF = 32;
  char buf[MAXBUF];
  strcpy(buf, "hello from ramfile0\n");

  // create file "ramfile0"
  fd0 = open("ramdisk0:/ramfile0", O_RDWR|O_CREATE);
  close(fd0);

  // link file "ramfile1" to "ramfile0"
  link("ramdisk0:/ramfile0", "ramdisk0:/ramfile1");

  printu("\n============= Task 1 =============\n");
  printu("link: ramfile0 ramfile1 success!");
  printu("\n==================================\n");

  // write to "ramfile0"
  fd0 = open("ramdisk0:/ramfile0", O_RDWR);
  mywrite(fd0, buf, strlen(buf)+1);
  printu("\nwrite to ramfile0: \n%s\n", buf);
  close(fd0);

  // Is ramfile1 also updated?
  fd1 = open("ramdisk0:/ramfile1", O_RDWR);
  myread(fd1, buf, MAXBUF);
  printu("read from ramfile1: \n%s\n", buf);
  close(fd1);

  // check the directory entry after linking
  printu("read directory ramdisk0:/\n");
  dir * dp = opendir("ramdisk0:/");
  struct dirent * d;
  while ( (d = readdir(dp)) != NULL ) {
    printu("dirent (inum, name): (%d, %s)\n", d->inum, d->name);
  }
  closedir(dp);

  // // removing a file actually calls unlink
  // unlink("ramdisk0:/ramfile0");

  // printu("\n============= Task 2 =============\n");
  // printu("unlink: ramfile0 success!");
  // printu("\n==================================\n");
  
  // // Is "ramfile1" still accessable?

  // fd1 = open("ramdisk0:/ramfile1", O_RDWR);
  // printu("\nhere\n");
  // myread(fd1, buf, MAXBUF);
  // printu("\nread from ramfile1: \n%s\n", buf);
  // close(fd1);

  exit(0);
  return 0;
}